#!/bin/bash
cd "$( dirname "${BASH_SOURCE[0]}" )"

count=0
w=405
h=720

unset WAYLAND_DISPLAY
unset DISPLAY

export DISPLAY=:1
weston --width=$w --height=$h >/dev/null 2>&1 &
sleep 5
export WAYLAND_DISPLAY=wayland-1

waydroid session start &
sleep 10
waydroid show-full-ui
sleep 60

waydroid app launch com.mihoyo.hyperion
sleep 20

for ((i=0;i<3;i++)); do
	adb exec-out screencap -p > ./autorecord/${count}.png
	ocr_text=$(tesseract ./autorecord/${count}.png stdout --psm 6 -l chi_sim wordstrbox | sed ':a; s/\(^[^#]*#[^[:space:]]*\)[[:space:]]/\1/; ta')
	let count++

	result=$(echo "$ocr_text" | grep '签到福利')
	if [[ -n "$result" ]]; then
		adb shell input tap 120 125
		sleep 30
		break
	fi

	result=$(echo "$ocr_text" | grep '我知道了\|下次再说')
	if [[ -n "$result" ]]; then
		read _ x1 y1 x2 y2 _ <<< "$result"
		adb shell input tap $(((x1+x2)/2)) $((h-(y1+y2)/2))
		sleep 10
	fi
done

for ((i=0;i<8;i++));do
	adb shell input tap $((73+i/2*86)) $((302+i%2*100))
	sleep 5
	adb exec-out screencap -p > ./autorecord/${count}.png
	result=$(tesseract ./autorecord/${count}.png stdout -l chi_sim --psm 6 wordstrbox | sed 's/ //g' | grep -o '签到成功')
	let count++
	[[ -n "$result" ]] && break
done

sleep 5

adb shell am force-stop com.mihoyo.hyperion
waydroid session stop
pkill weston

unset WAYLAND_DISPLAY
unset DISPLAY

echo $(date) $result | tee -a sign.log 
